package com.hxkcloud.nurse.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.utils.StringUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by william_sun on 2016-08-11.
 * Description:一般的显示webview的页面
 */
public class WebActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;
    private WebView webView;
    private ProgressBar pb_web;
    private String url;
    private String title;
    private boolean functionback;
    private String patientId = "";//患者ID
    private int patientInfoEdit = 0;//1编辑患者的症状诊疗 2基本信息 3既往史

    private final int code = 1001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_articledetail);

        url = getIntent().getStringExtra("url");
        title = getIntent().getStringExtra("title");
        functionback = getIntent().getBooleanExtra("functionback", false);
        patientInfoEdit = getIntent().getIntExtra("patientInfoEdit", 0);

        if (getIntent().hasExtra("patientId")) {
            patientId = this.getIntent().getStringExtra("patientId");
        }

        initView();
    }


    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);
        tv_title.setText(title);

        webView = (WebView) findViewById(R.id.webview_article);
        pb_web = (ProgressBar) findViewById(R.id.pb_article);

        WebSettings webSettings = webView.getSettings();

        webSettings.setJavaScriptEnabled(true); //支持js
        webSettings.setUseWideViewPort(true);  //将图片调整到适合webview的大小
        webSettings.setSupportZoom(true);  //支持缩放
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);  //设置缓存模式
        webSettings.setDomStorageEnabled(true);

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                pb_web.setProgress(newProgress);
                if (newProgress == 100)
                {
                    pb_web.setVisibility(View.INVISIBLE);
                }
                super.onProgressChanged(view, newProgress);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                System.out.println("url====" + url);
                if (patientInfoEdit == 1)
                {
                    StringUtil.zhugeTrack(mContext, "管理-患者信息-编辑-症状/诊疗-保存", "症状/诊疗保存数");
                } else if (patientInfoEdit == 2)
                {
                    StringUtil.zhugeTrack(mContext, "管理-患者信息-编辑-基本信息-保存", "基本信息保存数");
                } else if (patientInfoEdit == 3)
                {
                    StringUtil.zhugeTrack(mContext, "管理-患者信息-编辑-既往史-保存", "既往史点保存数");
                }

                if (url.contains("patientInfo"))
                {
                    finish();
                    return true;
                } else if (url.contains("backInfo") && functionback)
                {
                    finish();
                } else if (url.contains("isEditFzsj=yes"))
                {//是否编辑了复诊时间
//                    yijiahu://actionMethod?isEditFzsj=no
//                    Intent intent = new Intent(mContext, NoticeActivity.class);
                    Intent intent = new Intent(mContext, PatientBetterPlanActivity.class);
                    intent.putExtra("patientId", patientId);
                    intent.putExtra("isFuzhen", true);
                    startActivity(intent);
                    finish();
                    return true;
                } else if (url.contains("isEditFzsj=no"))
                {
                    finish();
                    return true;
                }else if(url.contains("childOptions"))
                {
                    String urlStr = "";

                    Matcher matcher = Pattern.compile("=").matcher(url);
                    if(matcher.find())
                    {
                        int index = matcher.start();

                        urlStr = url.substring(index+1,url.length());
                    }

                    Log.i("---DDDD->","urlStr:"+urlStr);

                    Intent intent = new Intent(mContext,WebListActivity.class);
                    intent.putExtra("url",urlStr);
                    startActivityForResult(intent,code);
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }

        });
        webView.loadUrl(url);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected void initListener() {
    }
    @Override
    protected void initData() {
    }
    @Override
    protected void updateByHandler(Message msg) {
    }
    @Override
    public void onClick(View v)
    {
        switch (v.getId()) {
            case R.id.iv_title_back:
                if (functionback)
                {
                    webView.loadUrl("javascript:back()");
                } else
                {
                    finish();
                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && functionback) {
            webView.loadUrl("javascript:back()");
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("---DDDD-->","requestCode:"+requestCode);
        Log.i("---DDDD-->","resultCode:"+resultCode);
        if(code == requestCode && resultCode == RESULT_OK)
        {
            Log.i("---DDDD-->","1111111111");
        }else
        {
            Log.i("---DDDD-->","22222222");
            webView.loadUrl("javascript:viewWillAppear()");
        }
    }
}
